package com.panda.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 执行顺序：
 * 正常：@Before--->@After--->@AfterReturning
 * 异常：@Before--->@After--->@AfterThrowing
 */
@Aspect
@Component
public class WebLogAcpect {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
    /**
     * 切点定义
     */
    @Pointcut("execution(* com.panda.*.service..*.*(..)) && !execution(* com.panda.common.service..*.*(..))")
    public void serviceLog(){

    }

    /**
     * 常和异常都会调用
     * @param joinPoint
     * @throws Throwable
     */
    @Before("serviceLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("doBefore");
    }

    /**
     * 正常和异常都会调用
     * @param joinPoint
     */
    @After(value = "serviceLog()")
    public void doAfterAdvice(JoinPoint joinPoint){
    	logger.info("doAfterAdvice");
    }

    /**
     * 正常返回时调用
     * @param joinPoint
     * @param ret
     * @throws Throwable
     */
    @AfterReturning(pointcut = "serviceLog()", returning = "ret")
    public void doAfterReturning(JoinPoint joinPoint, Object ret){
    	logger.info("doAfterReturning");
    }

    /**
     * 发生异常时调用
     * @param joinPoint
     * @param exception
     */
    @AfterThrowing(value = "serviceLog()",throwing = "exception")
    public void doAfterThrowingAdvice(JoinPoint joinPoint, Throwable exception){
    	logger.info("doAfterThrowingAdvice");
    }

}
